package pvt_chat

import (
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/db/mysql/db_main"
	"longmen/server/pkg/db/mysql/models/models_main"
	"longmen/server/pkg/db/redis/cache"
	"time"
)

/**
 * @author Jared
 * @description 结束会话
 * @createTime 13:30 2022/11/22
 **/
func EndChatSession(chatSession *models_main.PrivateChatSession) error {

	return nil
}

/**
 * @author Jared
 * @description 转代办状态
 * @createTime 14:29 2022/11/22
 **/
func TransWait(chatSession *models_main.PrivateChatSession) error {
	chatSession.Status = models_main.PrivateChatSessionStatusPending
	chatSession.AdminId = 0
	err := db_main.UpdatePrivateChatSession([]string{
		"status",
		"admin_id",
	}, chatSession)
	return err
}

/**
 * @author Jared
 * @description 接入代办会话
 * @createTime 18:12 2022/11/22
 **/
func WiringChatSession(adminId int, chatSession *models_main.PrivateChatSession) error {
	chatSession.Status = models_main.PrivateChatSessionStatusWiring
	chatSession.AdminId = adminId
	chatSession.IsDeleted = false
	err := db_main.UpdatePrivateChatSession([]string{
		"status",
		"admin_id",
		"is_deleted",
	}, chatSession)
	return err
}

/**
 * @author Jared
 * @description 统计用户数+1
 * @createTime 15:02 2022/11/22
 **/
func IncrPrivateChatUserNum(userId int, history *models_main.PrivateChatHistory) {
	count, err := cache.GetLiveUserCount(history.Id)
	if err != nil {
		global.Log.Error(err)
		return
	}
	count++
	cache.SetLiveUserCount(userId, history.Id)

	history.PrivateChatNum = int(count)
	err = db_main.UpdatePrivateChatHistory([]string{
		"private_chat_num",
	}, history)
	if err != nil {
		global.Log.Error(err)
		return
	}
}

/**
 * @author Jared
 * @description 累计会话数+1
 * @createTime 15:43 2022/11/22
 **/
func IncrChatSessionNum(history *models_main.PrivateChatHistory) {
	history.SessionNum++
	err := db_main.UpdatePrivateChatHistory([]string{
		"session_num",
	}, history)
	if err != nil {
		global.Log.Error(err)
		return
	}
}

/**
 * @author Jared
 * @description 开启私聊会话
 * @createTime 16:22 2022/11/22
 **/
func StartNewSession(anchorId int, onlineDevice *OnlineDeviceRequest) (
	chatSession *models_main.PrivateChatSession,
	history *models_main.PrivateChatHistory,
	err error,
) {

	return
}

/**
 * @author Jared
 * @description 老会话结转新会话
 * @createTime 15:09 2022/12/3
 **/
func StartNewSessionFromOld(chatSession *models_main.PrivateChatSession) (
	newSession *models_main.PrivateChatSession,
	history *models_main.PrivateChatHistory,
	err error,
) {
	history, err = db_main.GetPrivateChatHistoryById(chatSession.PrivateChatHistoryId)
	if err != nil {
		return
	}

	defer func() {
		IncrPrivateChatUserNum(chatSession.UserId, history)
		IncrChatSessionNum(history)
	}()

	newSession = &models_main.PrivateChatSession{
		CreatedAt:            time.Now(),
		PrivateChatHistoryId: chatSession.PrivateChatHistoryId,
		LiveId:               chatSession.LiveId,
		AnchorId:             chatSession.AnchorId,
		UserId:               chatSession.UserId,
		DeviceId:             chatSession.DeviceId,
		Device:               chatSession.Device,
		Ip:                   chatSession.Ip,
		ChatDuration:         0,
		AdminId:              0,
		Status:               models_main.PrivateChatSessionStatusPending,
		IsDeleted:            false,
	}
	err = db_main.InsertPrivateChatSession(newSession)
	if err != nil {
		return
	}

	//cache.SetPrivateChatSession(newSession.AnchorId, newSession.UserId, newSession)
	return
}

const (
	AdminReplyTimeLimit = 10 //minute
	UserReplyTimeLimit  = 10 //minute
)

// 聊天记录
type PrivateChatSingleMsg struct {
	Id                   int                                       //唯一id
	PrivateChatSessionId int                                       //会话id
	LiveId               int64                                     //直播间id
	AnchorId             int                                       //主播id
	UserId               int                                       //用户id
	CreatedAt            time.Time                                 //创建时间
	FromWho              int                                       //最后回复角色 0-用户 1-管理员
	MsgContent           api_models.GetPrivateChatRecordDetailResp //单次消息内容
}
